//
//  WZPBaseClassController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/14.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPBaseClassController.h"


static NSString * ID = @"cell";

@interface WZPBaseClassController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,weak) UIScrollView * topView;
@property (nonatomic,weak) UICollectionView * bottomView;
@property (nonatomic,weak)UIButton *selectedBtn;
@property (nonatomic,strong) NSMutableArray *bottons;
@property (nonatomic,weak)UIView *underLineView;
@property (nonatomic,assign)BOOL isheader;


@end

@implementation WZPBaseClassController


-(NSMutableArray * )bottons{
    if (_bottons == nil) {
        _bottons = [NSMutableArray array];
        
    }
    return _bottons;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加底部的View
    [self setUpBottomView];
    // 添加顶部的View(先添加底部的View,避免被覆盖)
    [self setUpView];
    // 取消额外的滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
}
// 每次显示的时候调用
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (_isheader == NO) {
        [self setUpTitleButton];
        _isheader = YES;
    }


}
// 添加子控制器的标题
-(void)setUpTitleButton{
    // 有几个子控制器就添加几个标题
    NSInteger count = self.childViewControllers.count;
    // 设置标题个位置
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = WZPScreenW / count;
    CGFloat btnH = 44;
    for (int i = 0; i < count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        // 设置按钮字体颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        // 设置字体大小
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        
        UIViewController * vc = self.childViewControllers[i];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        btnX = i * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [_topView addSubview:btn];
        // 添加按钮进数组
        [self.bottons addObject:btn];
        // 监听按钮点击
        [btn addTarget:self action:@selector(titleCilck:) forControlEvents:UIControlEventTouchUpInside];
        // 默认选中第一个
        if (i == 0) {
            UIView * underLineView = [[UIView alloc]init];
            underLineView.backgroundColor = [UIColor redColor];
            // 让按钮文字自适应,方便 设置下划线
            [btn.titleLabel sizeToFit];
            underLineView.wzp_width = btn.titleLabel.wzp_width;
            CGFloat h = 2;
            underLineView.wzp_height = h;
            underLineView.wzp_y = btn.wzp_height - h;
            underLineView.wzp_centerX = btn.wzp_centerX;
            _underLineView = underLineView;
            [_topView addSubview:underLineView];
            
            [self titleCilck:btn];
        }
    }
}
// 点击标题调用
-(void)titleCilck:(UIButton *)btn {
    
    // 获取角标
    NSInteger i = btn.tag;
    // 选中按钮
    [self selButton:btn];
    // 调转对应界面
    CGFloat offsetX = i * WZPScreenW;
    _bottomView.contentOffset = CGPointMake(offsetX, 0);
    
}
#pragma mark 选中按钮
-(void)selButton:(UIButton * )btn{
    
    _selectedBtn.selected = NO;
    btn.selected = YES;
    _selectedBtn = btn;
    // 移动下划线位置
    
    [UIView animateWithDuration:0.3 animations:^{
        _underLineView.wzp_centerX = btn.wzp_centerX;
    }];
}
#pragma mark 滚动完成调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger page = scrollView.contentOffset.x / WZPScreenW;
    // 获取按钮
    UIButton * button = self.bottons[page];
    
    // 选中按钮
    [self selButton:button];
    
    
}
// 创建顶部View
-(void)setUpView{
    
    // 顶部采用 UIScrollView
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WZPScreenW, 44)];
    scrollView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:scrollView];
    _topView = scrollView;
    
}
// 添加底部的View
-(void)setUpBottomView{
    
    // 设置自动布局
    UICollectionViewFlowLayout * layout = ({
        
        layout = [[UICollectionViewFlowLayout alloc]
                  init];
        layout.itemSize = CGSizeMake(WZPScreenW, WZPScreenH);
        // 修改滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // 取消间距
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout;
    });
    
    //底部采用UICollectionView
    UICollectionView * collectView = ({
        
        collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WZPScreenW, WZPScreenH) collectionViewLayout:layout];
        _bottomView = collectView;
        collectView.dataSource = self;
        collectView.delegate = self;
        // 开启分页
        collectView.pagingEnabled = YES;
        // 注册cell
        [collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
        
        collectView;
    });
    
    [self.view addSubview:collectView];
    
    
}
#pragma mark UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.childViewControllers.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    // 移除之前的cell
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController * vc = self.childViewControllers[indexPath.row];
    //添加到View
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

@end
